class Item {
    constructor(element) {
        this._name = String(element);
        this._value = Number(element);
        this._prev = null;
        this._next = null;
    }
}

//创建双链表
class DuobleList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.count = 0;
    }
    //getElement
    getElement(index) {
        let current = this.head;
        for (let i = 0; i < index; i++) {
            current = current._next;
        }
        return current;
    }
    //add添加
    add(element) {
        //创建节点
        let node = new Item(element)
        //双链表没有一个元素
        if (this.head === null) {
            this.head = node;
            this.tail = node;
        } else {//双链表中已有元素
            let last = this.tail;
            this.tail = node;
            last._next = node;
            node._prev = last;
        }
        this.count++;
    }
    //查找下标
    indexOf(ele) {
        let current = this.head;
        for (let i = 0; i < this.count; i++) {
            if (ele === current._value) {
                return i
            }
            current = current._next;
        }
    }
    //插入
    insert(ele, element) {
        //ele上一个元素 element 插入的元素
        //创建节点
        let node = new Item(element)
        //查找上一个元素的位置
        let index = this.indexOf(ele)
        //表头插入
        if (index === 0) {
            if (this.head === null) {
                this.head = node;
                this.tail = node;
            } else {
                //第一个
                let current = this.head;
                //第一个的下一个
                let next = current._next;

                node._prev = current;
                current._next = node;

                next._prev = node;
                node._next = next;
            }
        } else if (index === this.count) {//表尾插入
            let last = this.tail;

            last._next = node;
            node._prev = last;
        } else {//中间插入
            //找到当前位置节点
            let current = this.getElement(index);
            //当前节点的下一个
            let next = current._next;
            node._prev = current;
            current._next = node;
            next._prev = node;
            node._next = next;
        }
        this.count++;
    }
    //输出
    print() {
        console.log(list1)
    }
    //删除
    remove(element) {
        //表头删除
        //表尾删除
        //中间删除
        let index = this.indexOf(element)
        if (index >= 0 && index <= this.count) {
            let current = this.head;
            if (index === 0) {
                //第二个成为第一个
                this.head = current._next
            } else if (index === this.count) {
                //最后一个的前一个
                let prev = this.getElement(index - 1);
                //最后一个
                let current = this.getElement(index)

                prev._next = current._next
            } else {
                let prev = this.getElement(index - 1);
                //没改变之前 - 前一个的后一个
                let next = this.getElement(index + 1)
                //改变之后
                prev._next = next;
                next._prev = prev;
            }
            this.count--;
        } else {
            return undefined
        }
    }
    find(element) {
        let current = this.head;
        let isFind = false
        for (let i = 0; i < this.count; i++) {
            if (element === current._value) {
                isFind = true
            }
            current = current._next;
        }
        isFind ? console.log("存在") : console.log(null)
    }
    reveser_find(element) {
        let current = this.head;
        for (let i = 0; i < this.count; i++) {
            if (element === current._value) {
                console.log(element)
            }
            current = current._next;
        }
    }
    //返回头元素
    head() {
        return this.head
    }
    //返回尾元素
    tail() {
        return this.tail
    }
    //判断表是否为空
    isEmpty() {
        return this.count ? false : true
    }
    //清空表
    clear() {
        this.head  =null;
        this.tail = null;
        this.count = 0;
    }
}

let list1 = new DuobleList();

list1.add(3)
list1.add(5)
list1.insert(3, 4)
list1.insert(4, 8)

list1.remove(4)

list1.find(4)

list1.reveser_find(3)

// list1.clear()
list1.print()


